package com.tuanzi.loan.web.bean.family;

import com.base.common.log.LogService;
import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.family.LoanLargeDeliveryManagementService;
import com.tuanzi.loan.vo.request.family.LoanLargeDeliveryManagementSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeDeliveryManagementSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanLargeDeliveryManagementLazyModel;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.PRODUCT_MONEY;

/**
 * @Description:    大额外推投递管理后台
 * @Author:         xionghao
 * @CreateDate:      2018-09-10
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanLargeDeliveryManagementBean
        extends JSFListingBean<LoanLargeDeliveryManagementSearchRequest,LoanLargeDeliveryManagementSearchResponse> {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    LoanLargeDeliveryManagementService loanLargeDeliveryManagementService;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }
    private void initLazyDataModel() {
        $ = new LoanLargeDeliveryManagementSearchRequest();
        lazyDataModel = new LoanLargeDeliveryManagementLazyModel($);
    }
    public void onReset() {
        initLazyDataModel();
    }
    public void onRefresh(){
        lazyDataModel = new LoanLargeDeliveryManagementLazyModel($);
        addSuccessfullyMessage();
    }
    public void onCreateTarget() throws ParseException {
        $.setDelivermethod(null);
        $.setAmountinterval(null);
        $.setOwnner(null);
        $.setSort(1);
        $.setType(null);
    }

    public void onCreate(){
        String max = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountinterval()).getValue2();
        if (StringUtils.isNotBlank(max)){
            $.setMaxamount(Integer.parseInt(max));
        }
        String mix = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountinterval()).getValue1();
        if (StringUtils.isNotBlank(mix)) {
            $.setMinamount(Integer.valueOf(mix));
        }
        //创建
        try {
            if(null!=loanLargeDeliveryManagementService.saveLoanLargeDeliveryManagement($))
                 addSuccessfullyMessage();
            onReset();
        } catch (Exception e) {
            addErrorMessage("同一金额区间,投递优先级和甲方不可相同");
            LogService.error("同一金额区间,投递优先级和甲方不可相同",e);
        }

    }
    public void onEditTarget(LoanLargeDeliveryManagementSearchResponse response){
        $.setId((Long) response.getId());
        $.setOwnner(response.getOwnner());
        $.setAmountinterval(response.getAmountinterval());
        $.setDelivermethod(response.getDelivermethod());
        $.setSort(response.getSort());
        $.setType(response.getType());
        String max = dictionaryService.findByCodeAndName(PRODUCT_MONEY,response.getAmountinterval()).getValue2();
        if (StringUtils.isNotBlank(max)){
            $.setMaxamount(Integer.parseInt(max));
        }
        String mix = dictionaryService.findByCodeAndName(PRODUCT_MONEY,response.getAmountinterval()).getValue1();
        if (StringUtils.isNotBlank(mix)) {
            $.setMinamount(Integer.valueOf(mix));
        }
        try {
            $.setOperadate(sdf.parse(DateUtils.getCurrentDate()));
        } catch (ParseException e) {
            LogService.error("日期转换异常",e);
        }
    }

    public void onEdit(){
        String max = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountinterval()).getValue2();
        if (StringUtils.isNotBlank(max)){
            $.setMaxamount(Integer.parseInt(max));
        }
        String mix = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountinterval()).getValue1();
        if (StringUtils.isNotBlank(mix)) {
            $.setMinamount(Integer.valueOf(mix));
        }
        try {
            if (loanLargeDeliveryManagementService.updateLoanWholeSaleComment($)>0)
                addSuccessfullyMessage();
            onReset();
        } catch (Exception e) {
            addErrorMessage("编辑API失败");
            LogService.error("编辑API失败",e);
        }
    }

    public void onDelete(LoanLargeDeliveryManagementSearchResponse response){
        try {
            loanLargeDeliveryManagementService.deleteByid(response);
            addSuccessfullyMessage();
            onReset();
        } catch (Exception e) {
            addErrorMessage("删除API失败");
            LogService.error("删除API失败",e);
        }
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(PRODUCT_MONEY);
    }
}
